home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
4_10.lha
/
4_10
/
4_10c.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
963b
|
45 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ Exercise 4.10 (version 2)
/ Invert a two dimensional (n X n) array
/ using the Gauss-Jordan elimination method
/ with partial pivoting.
include <swap.h>
include <abs.h>
/ Find the maximum |A[i][diag]|, i >= diag.
/ Return FALSE if there are no non-zero entries.
tatic int dopivot(double **A, double **I,
int diag, int nelem)
double max = abs(A[diag][diag]);
int swaprow = diag;
for (int i = diag+1; i < nelem; i++)
{
double a_i = abs(A[i][diag]);
if (a_i > max)
{
max = a_i;
swaprow = i;
}
}
// if all are zero, then failure
if (max == 0.0)
return 0;
// swap row swaprow with row diag in A and I
if (swaprow != diag)
{
for (int j = diag; j < nelem; j++)
swap(A[diag][j], A[swaprow][j]);
for (j = 0; j < nelem; j++)
swap(I[diag][j], I[swaprow][j]);
}
return 1;